﻿// =================================================================== 
// 项目说明
//====================================================================
// lsc。@Copy Right 2019
// 文件： GovLogOperate.cs
// 项目名称：斑马车务
// 创建时间：2019-01-09
// 负责人：lsc
// ===================================================================
using System;
using System.Data;
using System.Collections.Generic;
using Dapper.Extensions;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Repository;
using System.Text;
using OF.Utility;
using CGT.Infrastructure.Dto;
using OF.Utility;

namespace CGT.Infrastructure.Service
{

    /// <summary>
    ///
    /// </summary>
    public class GovLogOperateService
    {
        protected GovLogOperateRepository repository;

        public GovLogOperateService()
        {
            repository = new GovLogOperateRepository();
        }

        #region 增删改查

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(GovLogOperate entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Insert(entity);
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public object Insert(List<GovLogOperate> entitys)
        {
            foreach (var entity in entitys)
            {
                entity.CheckCode = GetCheckCode(entity);
            }
            return repository.Insert(entitys);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Update(GovLogOperate entity)
        {
            entity.CheckCode = GetCheckCode(entity);
            return repository.Update(entity);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public bool Delete(GovLogOperate entity) { return repository.Delete(entity); }

        /// <summary>
        /// 删除所有
        /// </summary>
        /// <returns></returns>
        public bool DeleteAll() { return repository.DeleteAll(); }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public bool Delete(int primaryKey) { return repository.Delete(primaryKey); }

        /// <summary>
        /// 查询单个实体
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovLogOperate SingleOrDefault(object primaryKey) { return repository.SingleOrDefault(primaryKey); }


        /// <summary>
        /// 查询单个实体,如果存在多条则引发异常
        /// </summary>
        /// <param name="primaryKey">主键</param>
        /// <returns></returns>
        public GovLogOperate Single(object primaryKey) { return repository.Single(primaryKey); }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public List<GovLogOperate> QueryAll()
        {
            return repository.QueryAll();
        }

        #endregion

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="incidentType"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Page<GovLogOperateDto> GetList(ScLogOperate condition)
        {
            Page<GovLogOperateDto> list = repository.GetList(condition);
            foreach (var item in list.rows)
            {
                item.CurrentCheckCode = GetCheckCode(item);
            }
            return list;
        }

        /// <summary>
        /// 导出报表
        /// </summary>
        /// <param name="condition"></param>
        public void ImportDataToExcel(ScLogOperate condition)
        {
            string[] caption = { "模块", "用户", "事件", "来源", "事件类型", "结果", "发生时间", "描述" };
            string[] filed = { "ModuleName","UserName", "Incident", "Source", "IncidentTypeName", "Result", "CreatedDate", "Description" };
            Dictionary<int, string[]> dicRows = new Dictionary<int, string[]>();
            Page<GovLogOperateDto> list = GetList(condition);
            ExcelHelper.ExportDataSetToFile<GovLogOperateDto>(caption, list.rows, filed, "操作日志.xlsx");
        }

        /// <summary>
        /// 查询统计信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<LogOperateStatisticDto> SearchStatisticalInformation(ScLogOperate condition)
        {
            return repository.SearchStatisticalInformation(condition);
        }

        /// <summary>
        /// 获取校验码
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public string GetCheckCode(GovLogOperate entity)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(SiteUtility.GetAppSettings("LogCheckCode"));
            sb.Append(entity.UserId);
            sb.Append(entity.Incident);
            sb.Append(entity.Source);
            sb.Append(entity.IncidentType);
            sb.Append(entity.Result);
            sb.Append(entity.Description);
            sb.Append(entity.ModuleId);
            sb.Append(entity.CreatedDate.ToString("yyyyMMddHHmmss"));
            return MD5Helper.MD5Key(sb.ToString());
        }

        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="userId">操作人ID</param>
        /// <param name="incident">事件</param>
        /// <param name="incidentType">事件类型（1:增、2:删、3:该、4:查）</param>
        /// <param name="result">返回结果</param>
        /// <param name="description">描述</param>
        /// <param name="moduleId">模块ID</param>
        public void Info(int userId, string incident, int incidentType, string result, string description, int moduleId)
        {
            GovLogOperate log = new GovLogOperate
            {
                Description = description,
                Source = IPHelper.IPAddress,
                UserId = userId,
                Incident = incident,
                IncidentType = incidentType,
                ModuleId = moduleId,
                Result = result,
                CreatedDate = DateTime.Now
            };
            Insert(log);
        }

        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="userId">操作人ID</param>
        /// <param name="incident">事件</param>
        /// <param name="incidentType">事件类型（1:增、2:删、3:该、4:查）</param>
        /// <param name="result">返回结果</param>
        /// <param name="description">描述</param>
        /// <param name="moduleId">模块ID</param>
        public void Info(int userId, string incident, int incidentType, string result, object oldModel, object newModel, int moduleId)
        {
            string description = CompareObject.GetObjectInfo(oldModel, newModel);
            Info(userId, incident, incidentType, result, description, moduleId);
        }


    }
	
}